<?php
declare (strict_types = 1);

namespace app\index\controller;

use think\facade\Db;
use think\facade\Cache;
use think\facade\Request;
use think\facade\Config;
use app\middleware\Auth;

class Index{
    
    protected $middleware = [Auth::class];
    public function getBanner(){
        if(Request::isPost()){
            $list=[];
            $activity=Db::table('activity')->field('id,title,cover')
            ->where([
                ['is_publish','=',1],
            // ['end_time','>',time()]
            ])
            ->order('id desc')->limit(0,5)->select();
            $banner=[];
            foreach ($activity as $k=>$v){
                $cover=json_decode($v['cover'],true);
                $banner[$k]=[
                    'image'=>isset($cover[0])?'https://darong.storage.1415926.com.cn'.$cover[0]:'https://uviewui.com/swiper/swiper1.png',
                    'title'=>$v['title'],
                    'url'=>'/pages/activity/detail?activityId='.$v['id']
                ];
            }
            $result=[
                'banner'=>$banner,
                'logo'=>getLogo()
            ];
            return json(['code'=>200,'message'=>'获取成功','result'=>$result,'success'=>true]);
        }
    }
    
    
    public function share(){
        if(Request::isPost()){
            $result=[
                'title'=>'观木院子-大荣木业',
                'imageUrl'=>'https://darong.storage.1415926.com.cn/activity/11/1.jpg',
                'path'=>'/pages/index/index'
            ];
            return json(['code'=>200,'message'=>'获取成功','result'=>$result,'success'=>true]);
        }
    }
    
    public function getIndex(){
        if(Request::isPost()){
            $result=\think\facade\Cache::store('redis')->get('MP_INDEX_CONTENT');
            $result=json_decode(json_encode($result),true);
            if(!$result){
                $result=[];
                $activity=Db::table('activity')->field('id,title')
                ->field("CONCAT('https://darong.storage.1415926.com.cn',JSON_UNQUOTE(JSON_EXTRACT(cover, '$[0]'))) as image")
                ->field("CONCAT('/pages/activity/detail?activityId=',id) as url")
                ->where([['is_publish','=',1]])
                ->order('id desc')->limit(0,5)->select()->toArray();
                $result['banner']=$activity;
                $result['logo']=getLogo();
                $result['share']=[
                    'title'=>'观木院子-大荣木业',
                    // 'imageUrl'=>'https://darong.storage.1415926.com.cn/activity/11/1.jpg',
                    'imageUrl'=>$activity[0]['image'],
                    'path'=>'/pages/index/index'
                ];
                $result['exhibits']=Db::table('exhibits')->field('id as exhibits_id,zone,customize_code,title,era,tex,check_in_at')
                ->field("CONCAT('https://darong.storage.1415926.com.cn',JSON_UNQUOTE(JSON_EXTRACT(cover, '$[0]'))) as cover")
                ->where([['is_featured','=',1],['is_hide','=',0]])
                ->page(1,6)->select()->toArray();
                \think\facade\Cache::store('redis')->set('MP_INDEX_CONTENT',$result,3600);
            }
            
            return json(['code'=>200,'message'=>'获取成功','result'=>$result,'success'=>true]);
        }
    }
}